# Maintainer: Lukas Fleischer <lfleischer@archlinux.org>
# Contributor: David Runge <dvzrv@archlinux.org>
# Contributor: Aleksey Filippov <sarum9in@gmail.com>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Sven-Hendrik Haase <sh@lutzhaase.com>
# Contributor: Thomas S Hatch <thatch45@gmail.com>
# Contributor: Geoffroy Carrier <geoffroy@archlinux.org>
# Contributor: Daniel J Griffiths <ghost1227@archlinux.us>

pkgbase='protobuf'
pkgname=('protobuf' 'python-protobuf')
pkgver=25.1
pkgrel=1
pkgdesc="Protocol Buffers - Google's data interchange format"
arch=('loong64' 'x86_64')
url='https://developers.google.com/protocol-buffers/'
license=('BSD')
depends=(
  'gcc-libs'
  'glibc'
  'zlib'
  'abseil-cpp'
)
makedepends=(
  'cmake'
  'gtest'
  'python-build'
  'python-installer'
  'python-setuptools'
  'python-wheel'
)
checkdepends=(
  python-pytest
  python-numpy
)
source=(https://github.com/protocolbuffers/protobuf/archive/v$pkgver/$pkgname-$pkgver.tar.gz
        https://github.com/protocolbuffers/protobuf/commit/2e62ef1e.patch
        soversion.patch
        $pkgbase-21.12-pep517.patch)  # let's not call setup.py like a script and just build...
sha512sums=('d2fad2188118ced2cd951bdb472d72cc9e9b2158c88eeca652c76332a884b5b5b4b58628f7777272fa693140753823584ea9c7924f1655b1d5a363f59bdf7a4c'
            'f42d9bd702abe2c7fc4dcb07d050376287ac60b0b7e2fde0d7a9e9df24a620866bee5fd7de2e3b216095376de47e1fe7443cca74c9a9e85c1a0bc42e8973a280'
            'f0813a415cff5639e4709400f15b0c5565294e7907ae164e620b76258734c643115d8e5170bf0e4aee264c347fb7e01ac4be60d19be2a91c0ce9c561dad8c8e9'
            'a297e74ee4f807b3fad7da7d0de6dd9647963521be66cd2a2370343f5bd191cbb38759157ac0cdb161a5893a30a10520a5098e88a292c800859af33db5cf7a41')

prepare() {
  patch -d $pkgbase-$pkgver -p1 < $pkgname-21.12-pep517.patch
  patch -d $pkgname-$pkgver -p1 < 2e62ef1e.patch # Fix cmake config compatibility mode
  patch -d $pkgbase-$pkgver -p1 < soversion.patch # Restore soversion
  sed -e 's|c++14|c++17|' -i $pkgbase-$pkgver/python/setup.py
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D protobuf_BUILD_SHARED_LIBS=ON
    -D protobuf_USE_EXTERNAL_GTEST=ON
    -D protobuf_ABSL_PROVIDER=package
    -S $pkgbase-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose

  cd $pkgbase-$pkgver/python
  LDFLAGS+=" -L${srcdir}/build" \
  PROTOC="$srcdir"/build/protoc \
  python -m build --wheel --no-isolation
}

check() {
  ctest --test-dir build

  cd $pkgbase-$pkgver/python
  pytest -vv google/$pkgbase/internal/ || true
}

package_protobuf() {
  replaces=('protobuf-cpp')
  provides=('libprotoc.so' 'libprotobuf.so' 'libprotobuf-lite.so')

  DESTDIR="$pkgdir" cmake --install build

  cd $pkgbase-$pkgver
  install -vDm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname"
  install -vDm 644 editors/protobuf-mode.el \
    -t "$pkgdir/usr/share/emacs/site-lisp/"
  install -vDm 644 editors/proto.vim \
    -t "${pkgdir}/usr/share/vim/vimfiles/syntax"
}

package_python-protobuf() {
  pkgdesc='Python 3 bindings for Google Protocol Buffers'
  depends=(
    "protobuf=$pkgver"
    'python'
  )

  python -m installer --destdir="$pkgdir" $pkgbase-$pkgver/python/dist/*.whl
  install -vDm 644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}
